package com.hongxun.design.sqlite.popwind;

import android.app.Activity;
import android.app.AlertDialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hongxun.design.MyApp;
import com.hongxun.design.R;
import com.hongxun.design.constant.Constant;
import com.hongxun.design.mytools.IconSourse;
import com.hongxun.design.mytools.ToastShow;
import com.hongxun.design.ui.headandbottonpanel.HeadSecondControlPanel;
import com.hongxun.design.utils.DataUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by cqcqwu 选择材料
 */

public class ChangeMaterial extends PopupWindow {
    private Handler handler;
    private Activity context;
    private int type;

    /**
     * 0activity
     * 1handler
     * 2RMaterialModel
     */
    public ChangeMaterial(Object... obj) {
        this.context = (Activity) obj[0];
        this.handler = (Handler) obj[1];
        this.type = (int) obj[3];

        View view = LayoutInflater.from(context).inflate(R.layout.pop_changematerial, null);
        ViewHolder holder = new ViewHolder(view);
        setContentView(view);
        holder.setData();
        setHeight(RelativeLayout.LayoutParams.MATCH_PARENT);
        setWidth(RelativeLayout.LayoutParams.MATCH_PARENT);
        setFocusable(true);
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        setBackgroundDrawable(dw);
        setAnimationStyle(R.style.pop_ranim);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
    }

    class ViewHolder {
        private EditText etcount;
        private TextView tv1, tv2, tv3, tv4, tv5;
        private HeadSecondControlPanel headsecond_layout;
        private LinearLayout layout, layoutOld;
        private List<String> listNames;

        ViewHolder(View view) {
            etcount = (EditText) view.findViewById(R.id.cm_et_c);
            tv1 = (TextView) view.findViewById(R.id.cm_tv1);
            tv2 = (TextView) view.findViewById(R.id.cm_tv2);
            tv3 = (TextView) view.findViewById(R.id.cm_tv3);
            tv4 = (TextView) view.findViewById(R.id.cm_tv4);
            tv5 = (TextView) view.findViewById(R.id.cm_tv5);
            layout = (LinearLayout) view.findViewById(R.id.cm_mainView);
            layoutOld = (LinearLayout) view.findViewById(R.id.cm_llay);

            String title = "模块材料编辑";
            if (type == Constant.EdMeterial) title = "点设材料编辑";

            headsecond_layout = (HeadSecondControlPanel) view.findViewById(R.id.setpoint_layout);
            headsecond_layout.initHeadPanel(DataUtil.getsecondbean("保存", View.VISIBLE, R.color.bar_right, null,
                    title, R.color.title, View.VISIBLE, IconSourse.BACK + "", View.VISIBLE));

            headsecond_layout.mleftTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                }
            });
            //保存
            headsecond_layout.setClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String xh = tv2.getText().toString().trim();
                    String count = etcount.getText().toString().trim();

                    Message message = handler.obtainMessage();
                    message.what = 6;
                    handler.sendMessage(message);
                    dismiss();
                }
            });
            listNames = new ArrayList<>();
            //类别选择
            layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    showPopupWindow();
                }
            });
            //减一位
            tv4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String count = etcount.getText().toString().trim();
                    double aDouble = 0;
                    try {
                        aDouble = Double.parseDouble(count);
                    } catch (Exception e) {
                        e.printStackTrace();
                        ToastShow.showShort(context, "数字格式不对");
                        return;
                    }
                    if (aDouble < 1.0) return;
                    aDouble -= 1.0;
                    etcount.setText(aDouble + "");
                }
            });
            //加一位
            tv5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String count = etcount.getText().toString().trim();
                    double aDouble = 0;
                    try {
                        aDouble = Double.parseDouble(count);
                    } catch (Exception e) {
                        e.printStackTrace();
                        ToastShow.showShort(context, "数字格式不对");
                        return;
                    }
                    aDouble += 1.0;
                    etcount.setText(aDouble + "");
                }
            });
        }

        public void setData() {
            layoutOld.setVisibility(View.GONE);

        }

        private void showPopupWindow() {
            View myView = LayoutInflater.from(context).inflate(R.layout.listview, null);
            AlertDialog.Builder dialog = new AlertDialog.Builder(context);
            dialog.setView(myView).create();
            final AlertDialog show = dialog.show();
            ListView lv = (ListView) myView.findViewById(R.id.lv);

            ArrayAdapter<String> adapter = new ArrayAdapter<>(context, R.layout.textview, listNames);
            lv.setAdapter(adapter);
            lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                    show.cancel();
                }
            });
        }
    }
}
